package com.hnjowe.util;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

/**
 * @缩放图片
 * @projectname：cocotour
 * @packagename：com.hnjowe.util
 * @filename：ZoomPicture.java
 * @date：2011-1-29 下午03:25:54
 * @author: wangxiuwu
 */
public class ZoomPicture {
	
	private static ZoomPicture instance = null;

	public ZoomPicture() {
		// TODO Auto-generated constructor stub
	}
	
	 /**
     * 返回一个ZoomPicture类的单一实例
     * @return m_instance
     */
    public synchronized  static ZoomPicture getInstance(){
            if(instance == null){
                    instance = new ZoomPicture();
            }
            return instance;
    }
	
	/**
	 * @缩放图片
	 * 按指定宽度，高度自适应缩放
	 * 按指定高度，宽度自适应缩放
	 * @param imgsrc		源图
	 * @param imgdist		目标图
	 * @param widthdist		目标图宽,当等于0时，宽度自适应
	 * @param heightdist	目标图高,当等于0时，高度自适应
	 */
	public void zoomPic(String imgsrc, String imgdist, int widthdist, int heightdist) {   
	    try {   
			File srcfile = new File(imgsrc);   
	        if (!srcfile.exists()) {   
	            return;   
	        }   
	        Image src = javax.imageio.ImageIO.read(srcfile);   
	        
	        float srcWidth = (float)(Math.round(src.getWidth(null)*100))/100;	//原图宽
	        float srcHeight = (float)(Math.round(src.getHeight(null)*100))/100;	//原图高
	        float ScaleWH = srcWidth/srcHeight;		//宽高比
	        
	        
	        //System.out.println("srcWidth="+srcWidth);
	        //System.out.println("srcHeight="+srcHeight);
	        //System.out.println("ScaleWH="+ScaleWH);
	        
	        float width = 0;	//缩放宽度
	        float height = 0;	//缩放高度

	        if(widthdist==0 || heightdist==0){
	        	if(widthdist==0 && heightdist<srcHeight){
	        		height = heightdist;
	        		width = height * ScaleWH;
	        	}else{
	        		height = srcHeight;
	        		width = srcWidth;
	        	}
	        	if(heightdist==0 && widthdist<srcWidth){
	        		width = widthdist;
	        		height = width / ScaleWH;
	        	}else{
	        		width = srcWidth;
	        		height = srcHeight;
	        	}
	        }else{
	        	width = widthdist;
	        	height = heightdist;
	        }
	        
	        //System.out.println("width="+width);
	        //System.out.println("height="+height);
	  
	        BufferedImage tag= new BufferedImage((int) width, (int) height, BufferedImage.TYPE_INT_RGB);   
	  
	        // 选择图像平滑度比缩放速度具有更高优先级的图像缩放算法
	        tag.getGraphics().drawImage(src.getScaledInstance((int)width, (int)height,  Image.SCALE_SMOOTH), 0, 0,  null);   
	        
	        //使用 Area Averaging 图像缩放算法。
	        //tag.getGraphics().drawImage(src.getScaledInstance(width, height, Image.SCALE_AREA_AVERAGING), 0, 0, null);
 
	           
	        FileOutputStream out = new FileOutputStream(imgdist);   
	        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);   
	        encoder.encode(tag);   
	        out.close();   
	  
	    } catch (IOException ex) {
	    	System.out.println(ex);
	    }   
	}
}
